from flask import Flask, render_template, redirect, url_for, request
from services.vars import server_config, surround_config, user_config, shop_config, area_config
import requests, json

app = Flask(__name__)

@app.route('/')
def root():
    return redirect(url_for('index'))

@app.route('/index')
def index():
    return render_template('index.html')

@app.route('/surroud')
def surround():
    return render_template('surround_page.html')

@app.route('/user_analysis')
def user_analysis():
    return render_template('user_analysis_page.html')

@app.route('/shop_analysis')
def shop_analysis():
    return render_template('shop_analysis_page.html')

@app.route('/area_analysis')
def area_analysis():
    return render_template('area_analysis_page.html')

@app.route('/surround_search', methods=['POST'])
def surround_search():
    latitude = request.form['arg_latitude']
    longitude = request.form['arg_longitude']
    with_children = request.form.get('arg_with_children')
    transportation = request.form['arg_transportaion']
    rating = request.form['arg_rating']
    if with_children == 'on':
        with_children = 'True'
    if with_children is None:
        with_children = 'False'
    args = {'rating': rating, 'latitude': latitude, 'longitude': longitude, 'with_children': with_children, 'transportation': transportation}
    print('{}'.format(args))
    res = requests.post('http://{}:{}/surround'.format(surround_config['host'], surround_config['port']), json=args)
    #print(res.text)
    shops = json.loads(res.text)
    shops_dict = []
    i = 0
    for shop in shops:
        tmp = {}
        tmp['name'] = shop[1]
        tmp['address'] = shop[2]
        tmp['stars'] = shop[8]
        tmp['categories'] = shop[17]
        tmp['latitude'] = shop[6]
        tmp['longitude'] = shop[7]
        shops_dict.append(tmp)
    for shop in shops_dict:
        print('=====shops dict: {}'.format(shop))
    return render_template('surround_result_page.html', shops_dict=shops_dict)

@app.route('/user_search', methods=['POST'])
def user_search():
    username = request.form['arg_username']
    args = {'username': username}
    print('{}'.format(args))
    res = requests.post('http://{}:{}/user_analysis'.format(user_config['host'], user_config['port']), json=args)
    #print(res.text)
    user = json.loads(res.text)
    print('=====user: {}'.format(user))
    return render_template('user_analysis_result_page.html', user=user)

@app.route('/shop_search', methods=['POST'])
def shop_search():
    shopname = request.form['arg_shopname']
    args = {'shopname': shopname}
    print('{}'.format(args))
    res = requests.post('http://{}:{}/'.format(shop_config['host'], shop_config['port']), json=args)
    #print(res.text)
    rating = res.text
    print('=====user: {}'.format(rating))
    return render_template('shop_analysis_result_page.html',shopname=shopname, rating=rating)

@app.route('/shop_rank', methods=['POST'])
def shop_rank():
    res = requests.post('http://{}:{}/sort'.format(shop_config['host'], shop_config['port']))
    #print(res.text)
    shops = json.loads(res.text)
    shops_dict = []
    i = 0
    for shop in shops:
        tmp = {}
        tmp['name'] = shop[1]
        tmp['address'] = shop[2]
        tmp['city'] = shop[3]
        tmp['stars'] = shop[8]
        tmp['categories'] = shop[17]
        shops_dict.append(tmp)
    for shop in shops_dict:
        print('=====shops dict: {}'.format(shop))
    return render_template('shop_analysis_result_page.html', shops_dict=shops_dict)

@app.route('/city_search', methods=['POST'])
def city_search():
    city = request.form['arg_city']
    args = {'city': city}
    print('{}'.format(args))
    res = requests.post('http://{}:{}/'.format(area_config['host'], area_config['port']), json=args)
    #print(res.text)
    json_shop = json.loads(res.text)
    shop = {}
    shop['name'] = json_shop[1]
    shop['address'] = json_shop[2]
    shop['city'] = json_shop[3]
    shop['stars'] = json_shop[8]
    shop['categories'] = json_shop[17]
    print('=====city: {}'.format(shop))
    return render_template('area_analysis_result_page.html',city=city, shop=shop)

@app.route('/city_pop_shop', methods=['POST'])
def city_pop_shop():
    res = requests.post('http://{}:{}/pop'.format(area_config['host'], area_config['port']))
    print(res.text)
    shops = json.loads(res.text)
    shops_dict = []
    i = 0
    for shop in shops:
        tmp = {}
        tmp['name'] = shop[1]
        tmp['address'] = shop[2]
        tmp['city'] = shop[3]
        tmp['stars'] = shop[8]
        tmp['categories'] = shop[17]
        shops_dict.append(tmp)
    for shop in shops_dict:
        print('=====shops dict: {}'.format(shop))
    return render_template('area_analysis_result_page.html', shops_dict=shops_dict)


if __name__ == '__main__':
    app.run(debug=True, host=server_config['host'], port=server_config['port'])